//
//  EFengOrganizationListView.m
//  WZEfengAndEtong
//
//  Created by wanzhao on 16/2/17.
//  Copyright © 2016年 wanzhao. All rights reserved.
//

#import "EFengOrganizationListView.h"
#import "CXmppRequestMgr.h"
#import "CDepartmentModel.h"
#import "UIImage+ImageWithColor.h"
#import "EFengOrganizationDepCell.h"
#import "CContactModel.h"

@interface EFengOrganizationListView ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic,strong)NSArray *array;
@property (nonatomic,assign)NSInteger row;
@end

@implementation EFengOrganizationListView

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    self = [super initWithFrame:frame style:style];
    if (self) {
        self.dataSource = self;
        self.delegate = self;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.row = 100000000;
    }
    return self;
}
-(NSArray *)array
{
    if (_array == nil) {
        _array = @[@"群",@"讨论组",@"我的联系人"];
    }
    return _array;
}
- (void)setTableViewList:(NSArray *)tableViewList
{
    _tableViewList = tableViewList;
    [self tableView:self didSelectRowAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return self.array.count;
     }
    else
    {
    return self.tableViewList.count;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 35.0f * (HARDWARE_SCREEN_HEIGHT - 44.0f - 64.0f - 49.0f) / (568.0f - 44.0f - 64.0f - 49.0f);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    EFengOrganizationDepCell *DepCell = [EFengOrganizationDepCell eFengOrganizationDepcellWithtableview:tableView];
    if (indexPath.section == 0) {
        DepCell.textLabel.text = self.array[indexPath.row];
        DepCell.isCellSelected = NO;
        if (indexPath.row == self.row) {
             DepCell.isCellSelected = YES;
        }
    }
    else {
    id dep = self.tableViewList[indexPath.row];
    if ([dep isKindOfClass:[CDepartmentModel class]]) {
        CDepartmentModel *dep1 = (CDepartmentModel*)dep;
        DepCell.textLabel.text = dep1.DepartName;
        DepCell.isCellSelected = dep1.isSelected;
    }
    else
    {
        CContactModel *contact = (CContactModel*)dep;
        DepCell.textLabel.text = contact.UserName;
        DepCell.isCellSelected = contact.isSelected;
    }
    }
    
    return DepCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        NSArray *cellArray = [tableView visibleCells];
        for (id cell in cellArray) {
            if ([cell isKindOfClass:[EFengOrganizationDepCell class]]) {
                EFengOrganizationDepCell *DepCell = (EFengOrganizationDepCell*)cell;
                DepCell.selected = NO;
                 DepCell.backgroundColor = RGBCOLORWITH16COLOR(0xe7f5ff);
            }
        }
        [self.tableViewList enumerateObjectsUsingBlock:^(CDepartmentModel  *obj, NSUInteger idx, BOOL *stop) {
            obj.isSelected = NO;
        }];
        self.row = indexPath.row;
        if ([self.organizationListViewDelegate respondsToSelector:@selector(EFengOrganizationListViewDidSelectedWithName:indexPath:)]) {
            NSString *model = self.array[indexPath.row];
            [self.organizationListViewDelegate EFengOrganizationListViewDidSelectedWithName:model indexPath:indexPath];
        }
    }
    else
    {
    self.row = 1000000;
    [self.tableViewList enumerateObjectsUsingBlock:^(id  obj, NSUInteger idx, BOOL *stop) {
        if ([obj isKindOfClass:[CDepartmentModel class]]) {
            CDepartmentModel *dep1 = (CDepartmentModel*)obj;
            dep1.isSelected = NO;
        }
        if ([obj isKindOfClass:[CContactModel class]]) {
            CContactModel *contact = (CContactModel*)obj;
            contact.isSelected = NO;
        }
    }];
    id obj = self.tableViewList[indexPath.row];
        if ([obj isKindOfClass:[CDepartmentModel class]]) {
            CDepartmentModel *dep1 = (CDepartmentModel*)obj;
            dep1.isSelected = YES;
        }
        if ([obj isKindOfClass:[CContactModel class]]) {
            CContactModel *contact = (CContactModel*)obj;
            contact.isSelected = YES;
        }


    if ([self.organizationListViewDelegate respondsToSelector:@selector(EFengOrganizationListViewDidSelectedWithModel:indexPath:)]) {
        CDepartmentModel *model = self.tableViewList[indexPath.row];
        [self.organizationListViewDelegate EFengOrganizationListViewDidSelectedWithModel:model indexPath:indexPath];
    }
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
}

@end
